package com.foolishbird.sdk.ui.fragment;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.foolishbird.sdk.core.HttpEngineApi;
import com.foolishbird.sdk.params.FloatNoticeResponseParams;
import com.foolishbird.sdk.params.FloatUserInfoResponseParams;
import com.foolishbird.sdk.params.ResponseParams;
import com.foolishbird.sdk.params.bean.FloatNoticeInfo;
import com.foolishbird.sdk.params.bean.FloatUserInfo;
import com.foolishbird.sdk.params.bean.State;
import com.foolishbird.sdk.ui.assist.OnFinishActivityListener;
import com.foolishbird.sdk.utils.MyFragmentManger;
import com.foolishbird.sdk.utils.ResUtils;
import com.foolishbird.sdk.utils.WL;

import java.text.SimpleDateFormat;
import java.util.List;


public class FloatSettingFragment extends BaseHttpFragment implements View.OnClickListener {


    private LinearLayout phoneLayout,realnameLayout,versionLayout,pwdLayout;
    private TextView titleTxt;
    private ImageView backView;

    public static final FloatSettingFragment newInstance(Bundle bundle) {
        FloatSettingFragment fragment = new FloatSettingFragment();
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);
        int resource = ResUtils.getLayoutIdentifier(getActivity(), "fb_fragment_float_setting");
        View contentView =inflater.inflate(resource,null);
        backView = contentView.findViewById(getViewIdentifier("fb_fragment_login_back"));
        backView.setOnClickListener(this);
        titleTxt = contentView.findViewById(getViewIdentifier("fb_fragment_float_title"));
        titleTxt.setText("个人设置");
        realnameLayout = contentView.findViewById(getViewIdentifier("float_setting_realname"));
        realnameLayout.setOnClickListener(this);
        phoneLayout = contentView.findViewById(getViewIdentifier("float_setting_phone"));
        phoneLayout.setOnClickListener(this);
        pwdLayout = contentView.findViewById(getViewIdentifier("float_setting_pwd"));
        pwdLayout.setOnClickListener(this);
        versionLayout = contentView.findViewById(getViewIdentifier("float_setting_version"));
        versionLayout.setOnClickListener(this);



        return contentView;

    }

    @Override
    public void onResume() {
        super.onResume();
        Log.d("test","onResume");
        HttpEngineApi.getInstance().getUserInfo(requestCallbackHandler(FloatUserInfoResponseParams.class));
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if(!hidden){
            Log.d("test","onHiddenChanged");
            HttpEngineApi.getInstance().getUserInfo(requestCallbackHandler(FloatUserInfoResponseParams.class));
        }
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser){
            Log.d("test","setUserVisibleHint");
            HttpEngineApi.getInstance().getUserInfo(requestCallbackHandler(FloatUserInfoResponseParams.class));
        }
    }

    private FloatUserInfo userInfo;
    @Override
    public void onAsyncRequestResult(int flag, ResponseParams response, Object... params) {
        super.onAsyncRequestResult(flag, response, params);
        if (response != null) {
            int code = response.getState().getCode();
            if (State.SUCCESS == code) {
                if(response instanceof FloatUserInfoResponseParams){
                    userInfo =((FloatUserInfoResponseParams)response).getData().getUserInfo();

                }
            }
        }



        WL.d("FloatNoticeFragment",response.toString());
    }


    @Override
    public void onClick(View view) {
        if(view == realnameLayout){
            if(userInfo!=null&&!TextUtils.isEmpty(userInfo.getId_number())){
                Bundle bundle =new Bundle();
                bundle.putSerializable("userInfo",userInfo);
                MyFragmentManger.getInstance().gotoFragment(FloatSettingRealnameFragment.newInstance(bundle));
            }else{

            }
        }else if(view == phoneLayout){
            if(userInfo!=null&&!TextUtils.isEmpty(userInfo.getPhone())){
                Bundle bundle =new Bundle();
                bundle.putSerializable("userInfo",userInfo);
                MyFragmentManger.getInstance().gotoFragment(FloatPhonebindedFragment.newInstance(bundle));
            }else{
                MyFragmentManger.getInstance().gotoFragment(FloatPhoneUnbindFragment.newInstance(null));
            }
        }else if(view == pwdLayout){
            Bundle bundle =new Bundle();
            bundle.putSerializable("userInfo",userInfo);
            MyFragmentManger.getInstance().gotoFragment(FloatChangePwdChooseFragment.newInstance(bundle));
        }else if(view == versionLayout){
            Toast.makeText(getActivity(),"当前已经是最新版本",Toast.LENGTH_SHORT).show();
        }else if(view == backView){
            MyFragmentManger.getInstance().goBack(this);
        }

//        OnFinishActivityListener listener = getOnFinishActivityListener();
//        if (listener != null) {
//            listener.doFinishActivity(this);
//        }
    }

    private OnFinishActivityListener getOnFinishActivityListener() {
        if (!isNullContext()) {
            Activity activity = getActivity();
            if (activity instanceof OnFinishActivityListener) {
                return (OnFinishActivityListener) activity;
            }
        }
        return null;
    }
}
